package com.newyear.FitnessEquipmentManager.servlet;

import entity.FitnessEquipment;
import service.impl.FitnessEquipmentServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @author 吴阳基
 * @datetime 2023/11/11 15:06
 */
@WebServlet(urlPatterns = "/add")
public class AddServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=utf-8");

        FitnessEquipment fitnessEquipment = new FitnessEquipment();
        fitnessEquipment.setName(req.getParameter("name"));
        fitnessEquipment.setPrice(Double.parseDouble(req.getParameter("price")));
        fitnessEquipment.setCount(Integer.parseInt(req.getParameter("count")));
        fitnessEquipment.setStatus(Integer.parseInt(req.getParameter("status")));
        fitnessEquipment.setBuyTime(req.getParameter("buyTime"));

        FitnessEquipmentServiceImpl fitnessEquipmentService = new FitnessEquipmentServiceImpl();
        int add = fitnessEquipmentService.add(fitnessEquipment);

        PrintWriter out = resp.getWriter();
        if (add > 0){
            out.println("<script>alert('添加成功！');location.href = 'list'</script>");
        }else {
            out.println("<script>alert('添加失败！');history.back()</script>");
        }

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }
}
